package GUI.System;

/**
 * Login panel for all users of the GUI - customers and administrators alike.
 */
public class LoginControllerPanel extends javax.swing.JPanel {

    private GUIFrame frame;
    private String adminEmail = "admin@ta.dk";
    private String adminPassword = "teamawesome";

    /**
     * Custom constructor.
     */
    public LoginControllerPanel() {
        initComponents();
    }

    /**
     * Setter for GUIFrame reference.
     *
     * @param frame GUIFrame object.
     */
    public void setFrame(GUIFrame frame) {
        this.frame = frame;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        labWelcome = new javax.swing.JLabel();
        labInformation = new javax.swing.JLabel();
        labEmail = new javax.swing.JLabel();
        textEmail = new javax.swing.JTextField();
        labPassword = new javax.swing.JLabel();
        btnLogin = new javax.swing.JButton();
        btnForgot = new javax.swing.JButton();
        btnSignup = new javax.swing.JButton();
        labSignup = new javax.swing.JLabel();
        labError = new javax.swing.JLabel();
        textPassword = new javax.swing.JPasswordField();
        labAdminInfo1 = new javax.swing.JLabel();
        labAdminInfo2 = new javax.swing.JLabel();
        labAdminInfo3 = new javax.swing.JLabel();

        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        labWelcome.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        labWelcome.setText("Welcome to TA Charging");
        add(labWelcome, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 11, -1, -1));

        labInformation.setText("Enter your credentials to login.");
        add(labInformation, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 44, 197, -1));

        labEmail.setText("Email");
        add(labEmail, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 69, -1, -1));
        add(textEmail, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 89, 197, -1));

        labPassword.setText("Password");
        add(labPassword, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 120, -1, -1));

        btnLogin.setText("Login");
        btnLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoginActionPerformed(evt);
            }
        });
        add(btnLogin, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 176, -1, -1));

        btnForgot.setText("Forgot Password");
        btnForgot.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnForgotActionPerformed(evt);
            }
        });
        add(btnForgot, new org.netbeans.lib.awtextra.AbsoluteConstraints(94, 176, -1, -1));

        btnSignup.setText("Sign up");
        btnSignup.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSignupActionPerformed(evt);
            }
        });
        add(btnSignup, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 236, -1, -1));

        labSignup.setText("New User?");
        add(labSignup, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 216, -1, -1));

        labError.setForeground(new java.awt.Color(255, 0, 51));
        labError.setText("Wrong email or password");
        labError.setVisible(false);
        add(labError, new org.netbeans.lib.awtextra.AbsoluteConstraints(128, 216, -1, -1));
        add(textPassword, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 150, 200, -1));

        labAdminInfo1.setFont(new java.awt.Font("Dialog", 1, 11)); // NOI18N
        labAdminInfo1.setForeground(new java.awt.Color(0, 0, 153));
        labAdminInfo1.setText("To log in as admin:");
        add(labAdminInfo1, new org.netbeans.lib.awtextra.AbsoluteConstraints(290, 70, -1, -1));

        labAdminInfo2.setForeground(new java.awt.Color(0, 0, 153));
        labAdminInfo2.setText("Email: admin@ta.dk");
        add(labAdminInfo2, new org.netbeans.lib.awtextra.AbsoluteConstraints(290, 90, -1, -1));

        labAdminInfo3.setForeground(new java.awt.Color(0, 0, 153));
        labAdminInfo3.setText("Password: teamawesome");
        add(labAdminInfo3, new org.netbeans.lib.awtextra.AbsoluteConstraints(290, 110, -1, -1));
    }// </editor-fold>//GEN-END:initComponents

    /**
     * If values from input fields matches those of an administrator, log in as
     * such. Otherwise, search database for matching email and password. If
     * found, log in as customer.
     *
     * @param evt ActionEvent
     */
    private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoginActionPerformed
        StringBuilder sb = new StringBuilder();
        String password = sb.append(textPassword.getPassword()).toString();

        if (textEmail.getText().equals(adminEmail) && password.equals(
                adminPassword)) {
            labError.setVisible(false);
            frame.changePanel("card11");
            textEmail.setText("");
            textPassword.setText("");
        } else if (frame.cManager.loggedInAs(textEmail.getText().toLowerCase(),
                password.toLowerCase()) == true) {
            if (frame.cManager.getLoggedInUser().getAccountStatus().equals(
                    "Disabl")) {
                System.err.println("Account is disabled");
                labError.setVisible(true);
            } else {
                labError.setVisible(false);
                frame.changePanel("card2");
                textEmail.setText("");
                textPassword.setText("");
            }
        } else {
            labError.setVisible(true);
        }

    }//GEN-LAST:event_btnLoginActionPerformed

    /**
     * Change to sign-up panel.
     *
     * @param evt ActionEvent
     */
    private void btnSignupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSignupActionPerformed
        frame.changePanel("card3");
    }//GEN-LAST:event_btnSignupActionPerformed

    /**
     * Change to panel for forgotten password.
     *
     * @param evt ActionEvent
     */
    private void btnForgotActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnForgotActionPerformed
        frame.changePanel("card6");
    }//GEN-LAST:event_btnForgotActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnForgot;
    private javax.swing.JButton btnLogin;
    private javax.swing.JButton btnSignup;
    private javax.swing.JLabel labAdminInfo1;
    private javax.swing.JLabel labAdminInfo2;
    private javax.swing.JLabel labAdminInfo3;
    private javax.swing.JLabel labEmail;
    private javax.swing.JLabel labError;
    private javax.swing.JLabel labInformation;
    private javax.swing.JLabel labPassword;
    private javax.swing.JLabel labSignup;
    private javax.swing.JLabel labWelcome;
    private javax.swing.JTextField textEmail;
    private javax.swing.JPasswordField textPassword;
    // End of variables declaration//GEN-END:variables
}
